package cn.bdqn.asecu.controller;

import cn.bdqn.asecu.service.AsecuProjectTypeService;
import cn.bdqn.asecu.service.AsecuRoleService;
import cn.bdqn.pojo.Role;
import cn.bdqn.pojo.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 项目类型
 * @author 8
 */
@Controller
public class ProjectTypeController {

    @Autowired
    AsecuProjectTypeService projectTypeService;

    @Autowired
    AsecuRoleService asecuRoleService;

    /**
     * 获取数据显示到页面
     * @param model
     * @return
     */
    @RequestMapping("xiangmuleixing-list")
    public String html(Model model){
        List<Type> typeList = projectTypeService.getData();
        model.addAttribute("typeList",typeList);
        return "xiangmuleixing-list";
    }

    /**
     * 添加页面
     * @param model
     * @return
     */
    @RequestMapping("xiangmuleixing-add")
    public String xiangmuleixing_add(Model model){
        List<Role> roleList = asecuRoleService.getRole();
        model.addAttribute("roleList",roleList);
        return "xiangmuleixing-add";
    }

    /**
     * 添加项目类型
     * @param name
     * @param type
     * @param role
     * @return
     */
    @RequestMapping("addProjectType")
    @ResponseBody
    public String addProjectType(String name,String type,String[] role){
        Type t = new Type();
        t.setName(name);
        t.setType(type);
        StringBuffer sb = new StringBuffer();
        for (String s : role) {
            sb.append(s+"、");
        }
        sb.deleteCharAt(sb.length()-1);
        t.setRole(sb.toString());
        boolean b = projectTypeService.save(t);
        return b?"200":"error";
    }

    /**
     * 删除项目类型
     * @param ids
     * @return
     */
    @RequestMapping("delProjectType")
    @ResponseBody
    public String delProjectType(Integer[] ids){
        projectTypeService.delProjectType(ids);
        return "200";
    }

}
